<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDepartments">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--分页工具条-->
		<el-col :span="24" class="toolbar">
            <el-pagination
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-size="10"
                    layout="total, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
		</el-col>

		<!--列表-->
		<el-table :data="wpermissions" border highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="权限名称"  sortable>
			</el-table-column>
			<el-table-column prop="url" label="请求地址"  sortable>
			</el-table-column>
			<el-table-column prop="descs" label="描述"  sortable>
			</el-table-column>
			<el-table-column prop="sn" label="权限标识"  sortable>
			</el-table-column>
			<el-table-column
					prop="menu.id"
					label="是否是菜单"
					width="100"
					:formatter="menuFormatter"
					filter-placement="bottom-end">
				<template slot-scope="scope">
					<el-tag
							:type="scope.row.menu.id > 0 ? 'primary' : 'success'"
							disable-transitions>{{scope.row.menu.id > 0 ? '是' : '否'}}</el-tag>
				</template>
			</el-table-column>

			<el-table-column label="操作" width="200" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="权限名称" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="请求地址" prop="url">
					<el-input v-model="addForm.url" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="descs">
					<el-input v-model="addForm.descs" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="权限标识" prop="sn">
					<el-input v-model="addForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="是否菜单" prop="sn">
                    <el-switch
                            v-model="value1"
							@change = "switchChange"
                            active-color="#13ce66"
                            inactive-color="#ff4949">
                    </el-switch>
				</el-form-item>
				<el-form-item label="选择菜单" prop="parent_id" v-if="value1 == true">
					<el-input readonly v-model="menuName" auto-complete="off"></el-input>
					<el-tree
							:data="data"
							node-key="id"
							ref="tree2"
							@node-click = "changeMenu"
							:default-expanded-keys="[2, 3]"
							:default-checked-keys="[5]">
					</el-tree>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>


		<!--修改的时候 弹出 编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="权限名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="请求地址" prop="url">
					<el-input v-model="editForm.url" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="描述" prop="descs">
					<el-input v-model="editForm.descs" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="权限标识" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="是否菜单" prop="sn">
					<el-switch
							v-model="value1"
							@change = "switchChange"
							active-color="#13ce66"
							inactive-color="#ff4949">
					</el-switch>
				</el-form-item>
				<el-form-item label="选择菜单" prop="parent_id" v-if="value1 == true">
					<el-input readonly v-model="menuName" auto-complete="off"></el-input>
					<el-tree
							:data="data"
							node-key="id"
							ref="tree2"
							@node-click = "changeMenu"
							:default-expanded-keys="[2, 3]"
							:default-checked-keys="[5]">
					</el-tree>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>

	export default {
		data() {
			return {
                filters:{
                    name:'' //搜索框默认为空
				},
                data: [],
                value1: true,
                menuName:"请选择",
                currentPage:1,//默认第一页
                total:0,// 默认0条数据
                sels: [],//列表选中列
                listLoading:false,//如果等于false 现在加载中效果
                editLoading:false,//修改加载中
				addLoading:false,//新增加载
                wpermissions:[], //所有部门
                editFormVisible:false,//默认不显示对话框
                addFormVisible:false,//新增对话框 是否显示 false 不显示新增对话框
                editForm:{
                    id:0,
                    menu:{id:0},
                    name:''
				},//修改form对象
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' },
                    ]
                },
                addForm:{
                    name:'',
                    menu:{id:0},
				},//新增form对象
                addFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' },
                    ]
                },
			}
		},
		methods: {
		    //分页工具条方法
            handleCurrentChange(val){
                //当前页
                this.currentPage = val;
                this.getDepartments();
            },
		    //开关状态改变
            switchChange(){
                if(this.addFormVisible && !this.value1) this.addForm.menu.id = 0
                if(this.editFormVisible && !this.value1) this.editForm.menu.id = 0
            },

            //获取所有的菜单
            getTree(){
                //发送axios请求到后端 查询部门
                // http://localhost:80/wmenue/list
                let param = {
                    name:"",
                    currentPage: 1,
                    pageSize: 1000,
                }
                this.$http.patch('/wmenue/queryPage',param).then(res =>{
                    this.data = res.data.rows;
                })
            },
            //前往指定页
            handleCurrentChange(val){
                this.currentPage = val;
                this.getDepartments();
            },
			//菜单节点被点击
            changeMenu(data,node,self){
                this.menuName = node.label;
                if(this.addFormVisible && this.value1){
                    this.addForm.url = node.data.url
                    this.addForm.menu.id = node.data.id;
                }
                if(this.editFormVisible && this.value1){
					this.editForm.url = node.data.url
					this.editForm.menu.id = node.data.id;
                }
            },
            //菜单修改
            menuFormatter(row,column,cellValue,index){
                if(cellValue>0) return "是";
                return "否";
            },
		    //查询所有的部门
            getDepartments(){
				//发送axios请求到后端 查询部门
				// http://localhost:80/wpermission/list
                let param = {
                    name:this.filters.name,
                    currentPage: this.currentPage,
                }
				this.$http.patch('/wpermission/queryPage',param).then(res =>{
                    this.total = res.data.total;
                    this.wpermissions = res.data.rows;
				})
			},
			//新增弹出对话框
            handleAdd(){
                //弹出表单
				this.addFormVisible =true;
			},
			//编辑弹出对话框
            handleEdit(index,row){
                //打开修改的对话框
				this.editFormVisible = true;
                row.url != null && this.value1
				//回显
				this.editForm = row;

			},
			//删除方法
            handleDel(index,row){
                this.$confirm('确认删除吗？', '提示', {}).then(() => {
                    let id = row.id
                    this.$http.delete('/wpermission/' + id).then(res => {
                        // {isSuccess:true,msg:'xxx'}
                        console.log(res.data);
                        let {success, message} = res.data;
                        if (success) {
                            this.$message({
                                message: message,
                                type: 'success'
                            });
                            this.getDepartments();
                        } else {
                            this.$message({
                                message: message,
                                type: 'error'
                            });
                        }
                    })
                })
			},
			//新增保存操作
			checkForm(obj){
                console.log(obj)
                if(!obj.url){
                    this.$message({
                        message: "url必须",
                        type: 'error'
                    });
                    return false;
                }
                if(this.value1 && obj.menu.id == 0){
                    this.$message({
                        message: "请选择菜单",
                        type: 'error'
                    });
                    return false;
				}
				return true;
			},
            //新增
            addSubmit: function () {
                if(!this.checkForm(this.addForm))return false;
                //提交之前的验证工作--elementui验证
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;
							//{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.addForm);
                            this.$http.put('/wpermission/save',para).then((res) => {
                                //去掉转圈圈
                                this.addLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['addForm'].resetFields();
                                    //关闭对话框
                                    this.addFormVisible = false;
                                    this.getDepartments();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
								}
                            });
                        });
                    }
                });
            },
			//新增保存
			editSubmit(){
                if(!this.checkForm(this.editForm))return false;
                //和新增类似
                //提交之前的验证工作--elementui验证
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.editLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('/wpermission/update',para).then((res) => {
                                //去掉转圈圈
                                this.editLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: message,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['editForm'].resetFields();
                                    //关闭对话框
                                    this.editFormVisible = false;
                                    this.getDepartments();
                                }else{
                                    this.$message({
                                        message: message,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });

			},
			//多选方法
            selsChange: function (sels) {
                this.sels = sels;
            },

		},
        created(){
            this.getTree()
        },
		mounted() {
			//挂载结束 el:#myDiv 所有vue内容 都渲染到DOM--虚拟DOM
			//发送请求
			this.getDepartments()
		}
	}

</script>

<style scoped>

</style>